package Controlador;

import Modelo.Figuras;
import Modelo.Semaforo;
import java.io.Serializable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MotorSemaforo extends Thread implements Serializable  {
	private static final long serialVersionUID = 5L;

   Vector<Figuras> fig;  
   Semaforo sem;
  int tiempo;

   public  MotorSemaforo (Semaforo s) {
       sem=s;
       tiempo=4000;
   }

  
public synchronized  void turnoA(){    //sincronizado con turno B porque modifican ambos turnoA
       while(sem.id % 2==1){
          sem.moverse(-10, 0);
       break;
       }
       while(sem.id % 2==0){
          sem.back();
       break;
       }

      }

public synchronized void turnoB(){

        while(sem.id % 2 == 0){
          sem.moverse(-10, 0);
       break;
       }
       while(sem.id % 2 == 1){
          sem.back();
       break;
       }
      }

    @Override
   public synchronized void run ()
   {
         
     while(true){
         this.turnoA();
            try {
                sleep(tiempo);
                
            } catch (InterruptedException ex) {
                Logger.getLogger(MotorSemaforo.class.getName()).log(Level.SEVERE, null, ex);
            }
            sem.noti();
                 
          this.turnoB();

          try {
                sleep(tiempo);
                
            } catch (InterruptedException ex) {
                Logger.getLogger(MotorSemaforo.class.getName()).log(Level.SEVERE, null, ex);
            }
          sem.noti();
      }
   }
    
}
